<?php
namespace Common\Model\Common;

use Common\Base\ModelBase;

class ZoneModel extends ModelBase
{
    /**
     * 得到
     *
     * @param int    $w        1:计数;  2:数据  3:计数&数据  4:一条数据
     * @param array  $search
     * @param string $fields
     * @param int    $page
     * @param int    $pageSize
     * @param string $by
     *
     * @return array
     */
    public function getZone($w=3, $search=array(), $fields='*', $page=0, $pageSize=0, $by='')
    {
        //查询条件
        $where =  ' where 1 ';
        if( ! empty($search))
        {
            isset($search['id']) && intval($search['id'])>0 && $where .= ' and id='.intval($search['id']);
            isset($search['pid']) && intval($search['pid'])>-1 && $where .= ' and pid='.intval($search['pid']);
            isset($search['level']) && intval($search['level'])>0 && $where .= ' and level='.intval($search['level']);
        }

        //排序
        $orderBy = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderBy .= 'sort asc';
        }else{
            $orderBy .= $by;
        }

        //SQL
        $sql['sql_ct'] = 'select count(id) as tot from '.getTable('common_zone').$where;
        $sql['sql']    = 'select '.$fields.' from '.getTable('common_zone').$where.$orderBy;

        return $this->getData($w, $sql, $page, $pageSize);
    }
}